<?php

namespace app\common\model;

use houdunwang\arr\Arr;
use think\Db;
use think\Loader;
use think\Model;
use think\Validate;
use houdunwang\crypt;

/**
 *  Goods 商品模型
 * Class Category
 * @package app\common\model
 */
class Goods extends Model
{
    //类型tid
    protected $pk = 'gid';
    //获取当前的数据表
    protected $table = 'shop_goods';

    //类型首页
    public function lists()
    {
        return Db::table('shop_goods')->select();

    }
    //类型属性的添加
    public function add($data)
    {
        //执行验证，填充数据 save（$data）: $post
        $result = $this->validate(true)->save($data);
        if(false === $result){
            //错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            //成功信息
            return ['valid'=>1,'msg'=>'添加成功'];
        }
    }
    //商品的编辑
    public function edit($data)
    {
        $result = $this->validate(true)->save($data, [$this->pk => $data['gid']]);
//        p($result);die;
        if ($result) {
            //执行成功
            return ['valid' => 1, 'msg' => '编辑成功'];
        } else {
            return ['valid' => 0, 'msg' => $this->getError()];
        }
    }
    //商品的删除
    public function del($gid)
    {
        $result =  Db::table('shop_goods')->where('gid',$gid)->delete();
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
            //dump($this->getError());
        }else{
            return ['valid'=>1,'msg'=>'删除成功'];
        }
    }

}
